package com.synjones.handsetS8;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;

import com.synjones.aamodule.AAModuleCallbackInterface;
import com.synjones.aamodule.AAModuleControl;
import com.synjones.aamodule.Bytes2Bmp;
import com.synjones.synjonesphone.BuildConfig;

/**
 * Created by zhaodianbo on 2016-4-25.
 */
public class S8FpModule implements FingerprintReaderInterface,AAModuleCallbackInterface {
    //声明设备
    AAModuleControl fpModule;
    Context context;
    Handler outHandler;
    static final int FLAG_NEED_CONTINUE=1;
    static final int FLAG_DONT_CONTINUE=0;
    Bitmap fpBmp;
    private int needContinue=FLAG_NEED_CONTINUE;

    public S8FpModule(Context context, Handler handler){
        this.context=context;
        fpModule=new AAModuleControl(this);
        outHandler=handler;
    }
    @Override
    public void powerOn() {
        fpModule.powerOn();

        if(S8PowerControl.COMPATIBLE_WITH_OLD_MACHINE) {
            // TODO: 2016-9-27 为了兼容老机器测试用，正式版本请使用setFingerPower()
            WtWdPowerUtils.setFingerPowerInclude3v3(context);
        }else {
            WtWdPowerUtils.setFingerPower(context);
        }

        try {
            Thread.sleep(500);//使模块上电完成
        } catch (InterruptedException e) {}
    }

    @Override
    public void powerOff() {
        close();
        fpModule.powerOff();
        if(S8PowerControl.COMPATIBLE_WITH_OLD_MACHINE) {
            // TODO: 2016-9-27  为了兼容老机器测试用，正式版本请使用closeFingerPower()
            WtWdPowerUtils.closeFingerPowerInclude3v3(context);
        }else {
            WtWdPowerUtils.closeFingerPower(context);
        }
    }

    @Override
    public int open() {
        int ret=fpModule.AAModuleOpen(0,"/dev/ttyMT1");
        fpModule.AAModuleSetEnrollImageCount(1);
        return ret;
    }

    public int open(int securityLevel) {
        int ret=fpModule.AAModuleOpen(securityLevel,"/dev/ttyMT1");
        fpModule.AAModuleSetEnrollImageCount(1);
        return ret;
    }
    @Override
    public int close() {
        return fpModule.AAModuleClose();
    }

    @Override
    public int cleanFpFeature() {
        return fpModule.AAModuleClear();
    }

    @Override
    public FpFeature getFpFeature(boolean showFpImage) {
        needContinue=FLAG_NEED_CONTINUE;
        fpBmp=null;
        byte fp_template[]=new byte[512];
        final int downloadImg=showFpImage?1:0;
        int ret=fpModule.AAModuleEnroll(fp_template,downloadImg);
        if(ret==512){
            int quality=fp_template[6]&0xff;
            return new FpFeature(fpBmp,quality,fp_template,"");
        }

        return new FpFeature(fpBmp,-1,null,"");
    }


    @Override
    public int setFpFeature(int toWhere, byte[] data) {
        return fpModule.AAModuleSetTemplate_2ndID(toWhere,data, 512, 0);
    }

    @Override
    public FpVerifyResult verifyFp(int sThreshold) {

        needContinue=FLAG_NEED_CONTINUE;
        int ret=fpModule.AAModuleVerify(0, 1);
        Log.e("AATest", "AAModuleVerify ret="+ret);
        if(ret>=0){
            int score=ret&0xff*100/255;
            int id=(ret>>8)&0xff;
            return new FpVerifyResult(id>0,score,id);
        }
        return new FpVerifyResult(false,0,0);
    }

    @Override
    public void cancel() {
        needContinue=FLAG_DONT_CONTINUE;
    }

    @Override
    public int calibration() {
        return fpModule.AAModuleCalibration();
    }


    @Override
    public int AAModuleMsgCallback(int msg, int param) {
        String message="";
        switch(msg){
            case AAModuleControl.MSG_QUERYCONTINUE:
                return needContinue;// No attempt to stop the operation
            case AAModuleControl.MSG_MOREIMAGE:
                message="请重新放置手指以获取图像";
                break;
            case AAModuleControl.MSG_SUCCESS:
                message="成功";
                break;
            case AAModuleControl.MSG_FAILURE:
                message="失败";
                break;
            case AAModuleControl.MSG_INVALID_VALUE:
                message="非法指令";
                break;
            case AAModuleControl.MSG_ERROR:
                message="错误";
                break;
            case AAModuleControl.MSG_VERIFY_START:
            case AAModuleControl.MSG_ENROLL_START:
                message="请将手指放到传感器上";
                break;
            case AAModuleControl.MSG_BADIMAGE:
                message="图像质量太低";
                break;
            case AAModuleControl.MSG_NOTEXIST:
                message="指纹ID不存在";
                break;
            case AAModuleControl.MSG_CAPTUREIMAGE:
                message="正在捕获图像……";
                break;
            case AAModuleControl.MSG_DETECTIMAGE:
                message="正在检测图像……";
                break;
            case AAModuleControl.MSG_SENSOR_FAILURE:
                message="传感器出错";
                break;
            case AAModuleControl.MSG_MEMORY_ERROR:
                message="模块内存出错";
                break;
            default:
                //message="AAModuleMsgCallback default\n";
                break;
        }
        //Log.e("AAModule","msg="+msg+message);
        if(outHandler!=null)
            outHandler.obtainMessage(MESSAGE_UPDATE_STATUS,message).sendToTarget();
        return 0;
    }

    @Override
    public void processImage(byte[] buffer, int width, int height) {
        Log.e("AAModule","processImage width="+width+",height="+height);
        Bytes2Bmp bytes2Bmp=new Bytes2Bmp(context);
        //bytes2Bmp.saveMyBitmap("/aaTest.png", bytes2Bmp.get8bitBitmap(buffer));
        fpBmp=bytes2Bmp.get8bitBitmap(buffer);
    }
}
